package com.itzol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itzol.config.Result;
import com.itzol.entity.Recharge;
import com.itzol.entity.Reduce;
import com.itzol.entity.Users;
import com.itzol.mapper.RechargeMapper;
import com.itzol.service.RechargeService;
import com.itzol.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements RechargeService {
    @Autowired
    private RechargeService service;
    @Autowired
    private UsersService usersService;
    @Override
    public Result recharge(Reduce reduce) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("Phone",reduce.getPhone());
        Users users = usersService.getOne(wrapper);
        users.setMoney(users.getMoney()+reduce.getMoney());
        usersService.saveOrUpdate(users);
        Recharge recharge = new Recharge();
        recharge.setMoney(reduce.getMoney());
        recharge.setRecharger(users.getId());
//        Calendar calendar = Calendar.getInstance();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        recharge.setCreate_date(formatter.format(calendar.getTime()));
         boolean b= service.save(recharge);
        if (b) {
            return Result.succ("加款成功");
        }
        else {
            return Result.erreo("erreo");
        }
    }
}
